package com.game.battleship;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.game.battleship.db.DbAdapter;

public class NewPlayerActivity extends Activity {
	
	EditText playerName_EditText;
	EditText password_EditText;
	
//	Spinner difficulty_Spinnner;
	ArrayAdapter<CharSequence> difficulty_ArrayAdapter;
	Button newPlayerGo_Button;
	
	DbAdapter dbAdapter;
	
	Context context;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.new_player);
        
        context = this.getApplicationContext();
        
        dbAdapter = new DbAdapter(context);
        
        playerName_EditText = (EditText)this.findViewById(R.id.playerName_EditText);
        
        password_EditText = (EditText)this.findViewById(R.id.password_EditText);
        //password_EditText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
        
        
//        difficulty_Spinnner = (Spinner)this.findViewById(R.id.difficulty_Spinnner);
//        difficulty_ArrayAdapter = ArrayAdapter.createFromResource(this, R.array.difficulties, android.R.layout.simple_spinner_item);
//        difficulty_ArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//        difficulty_Spinnner.setAdapter(difficulty_ArrayAdapter);
        
        newPlayerGo_Button = (Button)this.findViewById(R.id.newPlayerGo_Button);
        newPlayerGo_Button.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				String username = playerName_EditText.getText().toString();
				String password = password_EditText.getText().toString();
				boolean validationSucceeded = validateEditTextInputString(username) & validateEditTextInputString(password);
				if (validationSucceeded) {
					// store information of the new user to the database
					long newlyInsertedRowID = -1;
					try {
						newlyInsertedRowID = dbAdapter.insertUser(username, password, 0, 1);
						if (newlyInsertedRowID != -1) {
							Toast.makeText(context, "Welcome, " + username + "!", Toast.LENGTH_SHORT).show();
							// start the next activity
							Intent intent = new Intent(NewPlayerActivity.this, UserInfoActivity.class);
							intent.putExtra(UserInfoActivity.EXT_USERNAME, username);
							startActivity(intent);
						}
						else {
							Toast.makeText(context, "Error! Cannot add new user to the database.", Toast.LENGTH_SHORT).show();
						}
					} catch (SQLiteException ex) {
						Toast.makeText(context, ex.getMessage(), Toast.LENGTH_SHORT).show();
						ex.printStackTrace();
					}
					
				}
				else {
					Toast.makeText(context, "Invalid input data! The input only accepts alphanumeric characters.", Toast.LENGTH_SHORT).show();
				}
			}
        	
        });
	}

	private boolean validateEditTextInputString(String str) {
		// TODO Auto-generated method stub		
		if (isValidIdentifier(str)) {
			return true;
		}
		return false;
	}

	private boolean isValidIdentifier(String str) {
		// TODO Auto-generated method stub
		if (str == null) return false;
		int len = str.length();
		if (len == 0) return false;
		for (int i=0;i<len;++i) {
			char c = str.charAt(i);			
			if (!Character.isLetter(c) && !Character.isDigit(c)) {
				return false;
			}
		}
		return true;
	}

}
